#define _CRT_SECURE_NO_WARNINGS 1
#include <cmath>
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(nullptr) {
	}
};
class Solution {
public:
	ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2)
	{
		int len1 = 0, len2 = 0;
		ListNode* cur = pHead1;
		while (cur)
		{
			len1++;
			cur = cur->next;
		}
		cur = pHead2;
		while (cur)
		{
			len2++;
			cur = cur->next;
		}
		ListNode* cur1 = pHead1;
		ListNode* cur2 = pHead2;
		if (len2 > len1)
		{
			cur1 = pHead2;
			cur2 = pHead1;
		}
		int cnt = abs(len1 - len2);
		while (cnt--)
		{
			cur1 = cur1->next;
		}
		while (cur1 && cur2)
		{
			if (cur1 == cur2)
			{
				return cur1;
			}
			cur1 = cur1->next;
			cur2 = cur2->next;
		}
		return nullptr;
	}
};
